<?php

date_default_timezone_set('Asia/Shanghai'); 

$img = isset($_POST['Upload_img'])? $_POST['Upload_img'] : '';

if(!empty($img)){
	// 获取图片
	list($type, $data) = explode(',', $img);
	
	// 判断类型
	if(strstr($type,'image/jpeg')!==''){
		$ext = '.jpg';
	}elseif(strstr($type,'image/gif')!==''){
		$ext = '.gif';
	}elseif(strstr($type,'image/png')!==''){
		$ext = '.png';
	}

	// 文件保存路径
	$image_path = $_SERVER['DOCUMENT_ROOT']."\upimage\uploads\\".date("Y-m-d")."\\";
	mkdirs($image_path);// 生成的文件夹
	
	// 生成的文件名
	$image_name = time().rand(100,999).$ext;
	
	$image_url = '/upimage/uploads/'.date("Y-m-d").'/'.$image_name;
	
	// 生成文件
	file_put_contents($image_path.$image_name, base64_decode($data), true);
	
	// 返回
	header('content-type:application/json;charset=utf-8');
	$ret = array('Upload_img'=>$image_url);
	
	echo json_encode($ret);

}


function mkdirs($dir, $mode = 0777)
{
    if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
    if (!mkdirs(dirname($dir), $mode)) return FALSE;
    return @mkdir($dir, $mode);
} 
?>